package com.wanganui.douyin.config;

import lombok.Getter;
import lombok.Setter;
import org.skyer.douyin.service.AgentConfigService;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 抖音配置
 *
 * @author xtwang
 * @date 2025/3/14 上午10:27
 **/
@Getter
@Setter
@Configuration
public class DyConfiguration implements Serializable {

    /**
     * 抖音应用配置缓存
     */
    private final Map<String, List<AgentConfiguration>> AGENT_CACHE = new ConcurrentHashMap<>();

    private List<AgentConfiguration> agents;

    private AgentConfigService agentConfigService;

    private IAgentTokenService agentTokenService;

    /**
     * 抖音小程序密钥缓存
     */
    private final Map<String, DyAppletKey> DY_APPLET_KEY_CACHE = new ConcurrentHashMap<>();


    public DyConfiguration() {
    }

    public static DyConfiguration configuration() {
        return new DyConfiguration();
    }

    /**
     * 根据租户clientKey获取抖音应用配置
     *
     * @param clientKey 应用key
     * @return 抖音应用配置
     */
    public AgentConfiguration getAgentByClientKey(final String clientKey) {
        return Optional.ofNullable(AGENT_CACHE.get(clientKey)).orElseGet(ArrayList::new).stream().findFirst().orElse(new AgentConfiguration());
    }


    public void setAgents(List<AgentConfiguration> agents) {
        this.agents = agents;
        if (!CollectionUtils.isEmpty(agents)) {
            Map<String, List<AgentConfiguration>> collect = agents.stream().collect(Collectors.groupingBy(AgentConfiguration::getClientKey));
            AGENT_CACHE.putAll(collect);
        }
    }

    public void setAgentConfigService(AgentConfigService iAgentConfigService) {
        this.agentConfigService.setDyConfiguration(this);
    }

    public IAgentTokenService getAgentTokenService() {
        return agentTokenService;
    }

    public void setAgentTokenService(IAgentTokenService iAgentTokenService) {
        this.agentTokenService = iAgentTokenService;
    }

    /**
     * 获取抖音小程序密钥
     *
     * @param tenantId
     * @param clientKey
     * @return
     */
    public DyAppletKey getDyAppletKey(Integer tenantId, String clientKey) {
        String key = tenantId + "_" + clientKey;
        DyAppletKey dyAppletKey = DY_APPLET_KEY_CACHE.get(key);
        if (dyAppletKey != null) {
            return dyAppletKey;
        }
        AgentConfiguration agentConfiguration = getAgentByTenantId(tenantId, clientKey);
        dyAppletKey = new DyAppletKey();
        if (StringUtils.hasText(agentConfiguration.getApplicationPrivateKeyPath())) {
            dyAppletKey.setApplicationPrivateKey(DecryptUtils.loadRsaPrivateKey(agentConfiguration.getApplicationPrivateKeyPath()));
        }
        if (StringUtils.hasText(agentConfiguration.getApplicationPublicKeyPath())) {
            dyAppletKey.setApplicationPublicKey(DecryptUtils.loadRsaPublicKey(agentConfiguration.getApplicationPublicKeyPath()));
        }
        if (StringUtils.hasText(agentConfiguration.getPlatformPrivateKeyPath())) {
            dyAppletKey.setPlatformPrivateKey(DecryptUtils.loadRsaPrivateKey(agentConfiguration.getPlatformPrivateKeyPath()));
        }
        if (StringUtils.hasText(agentConfiguration.getPlatformPublicKeyPath())) {
            dyAppletKey.setPlatformPublicKey(DecryptUtils.loadRsaPublicKey(agentConfiguration.getPlatformPublicKeyPath()));
        }
        DY_APPLET_KEY_CACHE.put(key, dyAppletKey);
        return dyAppletKey;
    }
}
